using System;
using System.Collections.Generic;
using System.Text;
using DTO;
using DAO;
using System.Data;

namespace BUS
{
    /// <summary> 
    ///Author: nnamthach@gmail.com 
    /// <summary>
    public class ContractControl
    {
        #region Local Variable
        private ContractDataAccess _ContractDAO;
        #endregion Local Variable

        #region Method
        public ContractControl()
        {
            _ContractDAO = new ContractDataAccess();
        }

        public ContractInfo Get_Contract(
            String Contract
        , ref string sErr)
        {
            return _ContractDAO.Get_Contract(
            Contract
            , ref sErr);
        }

        public ContractInfo Get_Contract1(
            String Contract
        , ref string sErr)
        {
            return _ContractDAO.Get_Contract1(
            Contract
            , ref sErr);
        }

        public DataTable GetAll_Contract(ref string sErr)
        {
            return _ContractDAO.GetAll_Contract(ref sErr);
        }
        public DataTable GetAll_Contract1(ref string sErr)
        {
            return _ContractDAO.GetAll_Contract1(ref sErr);
        }

        public DataTable GetCond(String strCond, ref string sErr)
        {
            return _ContractDAO.GetCond(strCond, ref sErr);
        }

        public DataTable GetCond1(String strCond, ref string sErr)
        {
            return _ContractDAO.GetCond1(strCond, ref sErr);
        }

        public Int32 Add_Contract(ContractInfo obj, ref string sErr)
        {
            return _ContractDAO.Add_Contract(obj, ref sErr);
        }
        public Int32 Add_Contract1(ContractInfo obj, ref string sErr)
        {
            return _ContractDAO.Add_Contract1(obj, ref sErr);
        }

        public string Update_Contract(ContractInfo obj)
        {
            return _ContractDAO.Update_Contract(obj);
        }
        public string Update_Contract1(ContractInfo obj)
        {
            return _ContractDAO.Update_Contract1(obj);
        }

        public string Delete_Contract(
            String Contract
        )
        {
            return _ContractDAO.Delete_Contract(
            Contract
            );
        }
        public string Delete_Contract1(
            String Contract
        )
        {
            return _ContractDAO.Delete_Contract1(
            Contract
            );
        }
        public Boolean IsExist_Contract(
            String Contract
        )
        {
            return _ContractDAO.IsExist_Contract(
            Contract
            );
        }
        public Boolean IsExist_Contract1(
           String Contract
       )
        {
            return _ContractDAO.IsExist_Contract1(
            Contract
            );
        }

        /*  public string Delete_Contract(String arrID)
          {
              string kq = "";
              string[] arrStrID = arrID.Split(',');
              foreach (string strID in arrStrID)
              {
                  if (strID != "")
                  {
                      int ID = Convert.ToInt32(strID);
                      kq += _ContractDAO.Delete_Contract(ID);
                  }
              }
              return kq;
          } */


        public DataTable Search(String columnName, String columnValue, String condition, ref String sErr)
        {
            return _ContractDAO.Search(columnName, columnValue, condition, ref  sErr);
        }

        #endregion Method


        public decimal getTotalAmt(string busId, string code, ref string sErr)
        {
            if (busId == "HL") return _ContractDAO.getTotalAmtHL(code, ref  sErr);
            return _ContractDAO.getTotalAmtHV(code, ref  sErr);
        }

        public string updateTotal(string budId, string code)
        {
            if (budId == "HL") return _ContractDAO.updateTotalHL(code);
            else return _ContractDAO.updateTotalHV(code);
        }

        public DataTable getTotalAmts(string busId, string code, ref string sErr)
        {
            DataTable result = null;
            DataTable other = null;
            if (busId == "HL")
            {
                result = _ContractDAO.getTotalAmtsHL(code, ref sErr);
                other = _ContractDAO.getTotalAmtsHV(code, ref sErr);
            }
            else
            {
                other = _ContractDAO.getTotalAmtsHL(code, ref sErr);
                result = _ContractDAO.getTotalAmtsHV(code, ref sErr);
            }
            if (!result.Columns.Contains("SharedAmt")) result.Columns.Add("SharedAmt", typeof(decimal));
            if (!result.Columns.Contains("SharedRemaining")) result.Columns.Add("SharedRemaining", typeof(decimal));
            if (result.Rows.Count == 1 && other.Rows.Count == 1)
            {
                result.Rows[0]["SharedAmt"] = Convert.ToDecimal(result.Rows[0]["LimitAmt"]) + Convert.ToDecimal(other.Rows[0]["LimitAmt"]);
                result.Rows[0]["SharedRemaining"] = Convert.ToDecimal(result.Rows[0]["RemainingAmt"]) + Convert.ToDecimal(other.Rows[0]["RemainingAmt"]);
            }
            return result;
        }



        public void insertToSunSytem(ContractInfo info, ref string sErr, string connectString = "", string db = null)
        {
            _ContractDAO.insertToSunSytem(db, info, ref sErr, connectString);
        }
        public bool existsInSunSytem(ContractInfo info, ref string sErr, string connectString = null, string db = null)
        {
            return _ContractDAO.existsInSunSytem(db, info, ref sErr, connectString);
        }

        public DataTable GetAll(string busId, ref string sErr)
        {
            if (busId == "HL") return _ContractDAO.GetAll_Contract(ref sErr);
            return _ContractDAO.GetAll_Contract1(ref sErr);
        }

        public string Update(string busId, ContractInfo info)
        {
            if (busId == "HL") return Update_Contract(info);
            return Update_Contract1(info);
        }

        public bool IsExist(string busId, string ctNo)
        {
            if (busId == "HL") return IsExist_Contract(ctNo);
            return IsExist_Contract1(ctNo);
        }

        public string Delete(string busId, string ctNo)
        {
            if (busId == "HL") return Delete_Contract(ctNo);
            return Delete_Contract1(ctNo);
        }

        public int Add(string busId, ContractInfo info, ref string sErr)
        {
            if (busId == "HL") return Add_Contract(info, ref sErr);
            return Add_Contract1(info, ref sErr);
        }

        public object BeginTransaction(ref string sErr)
        {
            return _ContractDAO.BeginTransaction(ref sErr);
        }

        public bool CommitTransaction(ref string sErr)
        {
            return _ContractDAO.CommitTransaction(ref sErr);
        }

        public bool RollbackTransaction(ref string sErr)
        {
            return _ContractDAO.RollbackTransaction(ref sErr);
        }
    }
}
